package su.msk.jet.team01.chatik.server.commands;

import java.util.Map;
import java.util.Set;

import su.msk.jet.team01.chatik.server.data.Client;
import su.msk.jet.team01.chatik.server.data.DataManager;
import su.msk.jet.team01.chatik.server.data.Room;
import su.msk.jet.team01.chatik.server.data.RoomManager;

public class ListRoomsCommand extends Command {

	private static final String CLIENT_INFO_MSG_ROOMS_HEADER = "Active rooms: %s";

	public ListRoomsCommand() {
		super("rooms", 0);
	}

	@Override
	public void doExecute(final Client client, final String[] args) {
		client.send(CLIENT_INFO_MSG_ROOMS_HEADER, listRooms());
	}

	private String listRooms() {
		StringBuilder result = new StringBuilder();
		RoomManager roomManager = DataManager.getRoomManager();
		Map<String, Room> rooms = roomManager.getRooms();
		Set<String> keys = rooms.keySet();

		for (String key : keys) {
			result.append(key).append(", ");
		}
		result.delete(result.length() - 2, result.length());
		result.append('.');

		return result.toString();
	}
}
